<?php

namespace app\common\model;

use think\Model;

/**
 * 模型
 */
class PayLog extends Model
{
    protected $name = 'pay_log';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    public static function orderPayCreate($order, $transactionId='')
    {
        return self::create([
            'pay_type'       => 1,
            'pay_time'       => time(),
            'total_fee'      => (int)bcmul($order['pay_amount'], '100', 2),
            'transaction_id' => $transactionId,
            'order_sn'       => $order['order_sn'],
            'member_id'      => $order['member_id'],
            'openid'         => $order['openid'],
            'shop_id'        => $order['shop_id'],
            'status'         => 1,
            'remark'         => '订单支付'
        ]);
    }

    public static function rechargePayCreate($order, $transactionId='')
    {
        return self::create([
            'pay_type'       => 2,
            'pay_time'       => time(),
            'total_fee'      => $order['pay_amount'],
            'transaction_id' => $transactionId,
            'order_sn'       => $order['order_sn'],
            'member_id'      => $order['member_id'],
            'openid'         => $order['openid'],
            'shop_id'        => $order['shop_id'],
            'recharge_id'     => $order['recharge_id'],
            'status'         => 1,
            'remark'         => '充值'
        ]);
    }

    public static function payFinish($message)
    {
        return self::where('order_sn', $message['out_trade_no'])->update(['status' => 2,'transaction_id'=>$message['transaction_id'],'pay_amount'=>sprintf("%.2f",$message['total_fee']/100)]);
    }

}
